package com.cxd.web.frontend;

import com.cxd.entity.EbOrder;
import com.cxd.entity.EbProduct;
import com.cxd.entity.EbUser;
import com.cxd.entity.Ebcar;
import com.cxd.service.IEbOrderService;
import com.cxd.service.IEbProductService;
import com.cxd.service.IEbUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.alibaba.fastjson.JSON;

@Controller
@RequestMapping("/f-product")
public class EbProductController {
    @Autowired  //spring帮我们将ProductService的对象创建出来
    // 相当于ProductServiceImpl productServiceImpl = new ProductServiceImpl()
    private IEbProductService iEbProductService;
    @Autowired
    private IEbOrderService iEbOrderService;
    @Autowired
    private IEbUserService iEbUserService;

    @RequestMapping("/detail")
    public String detail(HttpSession session,Model model,Integer epId){
        Object userNow = session.getAttribute("lgnUser");
        if (userNow == null){
            return "redirect:/f-login/to_login";
        }
        model.addAttribute("userNow",userNow);
        System.out.println("epId==>"+epId);
        //1.根据商品id查询商品的详情
        EbProduct ebProduct = iEbProductService.selectOne(epId);
        //2.将商品信息传递给详情页面 product-new.jsp
        model.addAttribute("ebProduct",ebProduct);
        return "product-view";
    }
    @RequestMapping("/to-addcar")
    public String toAddcar(HttpSession session,Model model,String list){
        Object userNow = session.getAttribute("lgnUser");
        if (userNow == null){
            return "redirect:/f-login/to_login";
        }
        model.addAttribute("userNow",userNow);
        System.out.println(list);
        List<Ebcar> ebcarList = JSON.parseArray(list,Ebcar.class);
        List<EbProduct> ebProducts = new ArrayList<>();
        EbProduct ebProduct = null;
//        System.out.println(ebcarList);
//        System.out.println(ebcarList.get(0));
//        System.out.println(ebcarList.get(0).getEpId());
//        System.out.println(ebcarList.get(0).getNum());
        for (int i = 0;i<ebcarList.size();++i){
            ebProduct = iEbProductService.selectOne(ebcarList.get(i).getEpId());
            ebProduct.setNum(ebcarList.get(i).getNum());
            ebProducts.add(ebProduct);
        }
        model.addAttribute("list",ebProducts);
        return "shopping";
    }
    @RequestMapping("/addCar")
    public String addCar(HttpSession session,Model model,String list){
        System.out.println(list);
        Object user = session.getAttribute("lgnUser");
        if (user == null){
            return "redirect:/f-login/to_login";
        }
        System.out.println(1);
        EbUser ebUser = (EbUser) user;
        System.out.println(list);
        List<Ebcar> ebcarList = JSON.parseArray(list,Ebcar.class);
        EbProduct ebProduct = null;
        BigDecimal cost = new BigDecimal("0.00");
        System.out.println(3);
        for (int i = 0;i<ebcarList.size();++i){
            ebProduct = iEbProductService.selectOne(ebcarList.get(i).getEpId());
            System.out.println(4);
            ebProduct.setEpStock(ebProduct.getEpStock()-ebcarList.get(i).getNum());
            System.out.println(5);
            System.out.println(ebProduct);
            int num = iEbProductService.updateProduct(ebProduct);
            System.out.println(num);
            BigDecimal num1 = new BigDecimal(ebcarList.get(i).getNum());
            System.out.println(7);
            num1 = num1.multiply(ebProduct.getEpPrice());
            System.out.println(num1);
            cost = cost.add(num1);
            System.out.println(cost);
        }
        System.out.println(2);
        EbOrder ebOrder = new EbOrder();
        ebOrder.setEoUserId(ebUser.getEuUserId());
        ebOrder.setEoUserName(ebUser.getEuUserName());
        ebOrder.setEoUserAddress(ebUser.getEuAddress());
        ebOrder.setEoCost(cost);
        ebOrder.setEoStatus(1);
        ebOrder.setEoType(1);
        System.out.println(3);
        iEbOrderService.save(ebOrder);
        return "shopping-result";
    }
}
